package academic.concordia.bankofquebec.dao.client;

import java.util.List;

import org.hibernate.SessionFactory;

import academic.concordia.bankofquebec.entity.Client;

public class ClientDAO implements IClientDAO {
	
	private SessionFactory sessionFactory;

	@Override
	public void addClient(Client client) {
		getSessionFactory().getCurrentSession().save(client);
	}

	@Override
	public void updateClient(Client client) {
		getSessionFactory().getCurrentSession().update(client);
	}

	@Override
	public void deleteClient(Client client) {
		getSessionFactory().getCurrentSession().delete(client);
	}

	@Override
	public Client getClientById(String id) {
		List<?> list = getSessionFactory().getCurrentSession().createQuery("from Client where idNumber=?").setParameter(0, id).list();
		return (Client)list.get(0);
	}

	@Override
	public List<Client> getClients() {
		@SuppressWarnings("unchecked")
		List<Client> list = getSessionFactory().getCurrentSession().createQuery("from User").list();
		return (List<Client>) list;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Client validateClientAccess(Client client) {
		List<?> list = getSessionFactory().getCurrentSession().createQuery("from Client where idNumber=? and password=? and identificationType=?")
				.setParameter(0, client.getIdNumber()).setParameter(1, client.getPassword()).setParameter(2, client.getIdentificationType()).list();
		if(list != null && list.size() > 0)
			return (Client) list.get(0);
		else
			return null;
	}
}
